<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2021/11/20
 * Time: 13:41
 */
namespace app\api\service;

use app\api\controller\Common;
use think\Db;
use think\Exception;
use think\Session;

class ProjectService
{

    //详情
    static function detailService($where,$type=0,$field=null){
        $common = new Common();
        $detail = $common->public_select("project_pro",$where,$field)->find();
        if(empty($detail) & empty($type)){
            $queryTable = Db::query("show columns from us_project_pro"); //获取表结构
            foreach ($queryTable as $item) {
                $detail[$item['Field']] = "";
            };
        }
        return $detail;
    }

    //查询流程基本信息
    public function advanceDetail($where){
        $common = new Common();
        $detail =  $common->public_select("project_type",$where)->find();
        return $detail;
    }

    //推进计划详细信息
    public function proAdvanceDetail($where){
        $common = new Common();
        $detail = $common->public_select("project_pro_advance",$where)->find();
        if(empty($detail)){
            //查询表结构
            $queryTable = Db::query("show columns from us_project_pro_advance"); //获取表结构
            foreach ($queryTable as $item) {
                $detail[$item['Field']] = "";
            };
        }
        return $detail;
    }

    //操作推进计划操作
    public function advanceDoService($data,$where){
        $common = new Common();
        $id = $common->public_select("project_pro_advance",$where)->value("id");
        if(empty($id)){
            $data['addtime'] = date("Y-m-d H:i:s");
            $data['add_id'] = Session::get("id");
        }else{
            $data['updatetime'] = date("Y-m-d H:i:s");
            $data['update_id'] = Session::get("id");
            $data['id'] = $id;
        }
        $common->handle("project_pro_advance",$data,1);
    }


    //查询推进计划
    public function findDetail($where){
        $common = new Common();
        $detail = $common->public_select("project_pro",$where)->field("id,library,plan_category")->find();
        return $detail;
    }

    //查询项目计划流程
    public function projectTypeService($selType,$id,$year,$month,$library){
        $common = new Common();
        $project_type = [];
        if(!empty($selType)){
            $where['status'] = 1;
            $where['pid'] = 0;
            $where['type'] = $selType;
            $where['library'] = $library;
            $project_type = $common->public_select("project_type",$where,"id,name")->order("sort desc,id desc")->select();
            $advanceWhere['pro_id'] = $id;
            $advanceWhere['year'] = $year;
            //查询完成情况  project_pro_advance
            foreach ($project_type as $k=>$v){
                //查询子栏目
                $where['status'] = 1;
                $where['pid'] = $v['id'];
                $where['type'] = $selType;
                $child = $this->childService($where,$month,$id,$year);
                $childCount = count($child);
                if($childCount>0){
                    $project_type[$k]['childCount'] = $childCount;
                }
                $project_type[$k]['child'] = $child;
                //查询子栏目end


                $project_type[$k]['childCount'] = $childCount = count($child);
                $advanceWhere['type_id'] = $v['id'];
                foreach ($month as $k1=>$v1){
                    if(!empty($v1['name'])){
                        $advanceWhere['month'] = $v1['name'];
                        $detail =  $common->public_select("project_pro_advance",$advanceWhere)->find();
                        $month[$k1]['status'] = empty($detail)?0:1;

                        $police = "0";
                        if(!empty($detail)){
                            if($detail['declare_status']==1){
                                if($detail['declare_day']<0){
                                    $police = "1";
                                }
                                if(0<$detail['declare_day'] & $detail['declare_day']<30){
                                    $police = "2";
                                }
                                if($detail['declare_day']>=30){
                                    $police = "3";
                                }
                            }
                        }

                        $month[$k1]['police'] = $police;
                        $month[$k1]['declare_schedule'] = empty($detail)?"":$detail['declare_schedule'];
                        $month[$k1]['declare_status'] = empty($detail['declare_status'])?0:$detail['declare_status'];

                    }
                    $project_type[$k]['month'] = $month;
                }
            }
        }
        return $project_type;
    }



    //查询项目计划流程子项
    public function childService($where,$month,$id,$year){
        $common = new Common();
        $advanceWhere['pro_id'] = $id;
        $advanceWhere['year'] = $year;
        $child = $common->public_select("project_type",$where,"id,name")->order("sort desc,id desc")->select();

        $thisMonth = intval(date("m"));

        foreach ($child as $k=>$v){
            $advanceWhere['type_id'] = $v['id'];
            foreach ($month as $k1=>$v1){
                if(!empty($v1['name'])){
                    $advanceWhere['month'] = $v1['name'];
                    $detail =  $common->public_select("project_pro_advance",$advanceWhere)->find();

//                    $month[$k1]['status'] = empty($detail['status'])?0:$detail['status'];
                    $month[$k1]['status'] = empty($detail)?0:1;
                    $police = "0";
                    if($detail['declare_status']==1){
                        $police = "1";
                        /*if($detail['declare_day']<0){
                            $police = "1";
                        }
                        if(0<$detail['declare_day'] & $detail['declare_day']<30){
                            $police = "2";
                        }
                        if($detail['declare_day']>=30){
                            $police = "3";
                        }*/
                    }
                    if($detail['declare_status']==3){
                        $police = "3";
                    }

                    if($detail['declare_status']==0 && !empty($detail['content']) && $v1['name']<$thisMonth){
                        $police = "3";
                    }

                    if($detail['declare_status']==2 && $v1['name']<$thisMonth){
                        $police = "3";
                    }


                    $month[$k1]['police'] = $police;
                    $month[$k1]['declare_schedule'] = $detail['declare_schedule'];
                    $month[$k1]['declare_status'] = empty($detail['declare_status'])?0:$detail['declare_status'];
                }
                $child[$k]['month'] = $month;
            }
        }
        return $child;
    }

    //进度汇报
    static function progress($param)
    {
        $param["addtime"] = time();
        return Db::name("project_progress")->insert($param);
    }

    //进度详情
    static function problemInfo($where,$field = "id,end_time,content,status,images")
    {
        return Db::name("project_pro_advance_plan")->where($where)->field($field)->find();
    }

    //进度详情
    static function progressInfo($where)
    {
        return Db::name("project_progress")->where($where)->order("id desc")->field("id,progress,work,last_work,question")->find();
    }



}